<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2022-08-25 21:33:51
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\CommonUserTrait;
use App\Traits\MaterialTrait;
use App\Traits\SoftDeletesTrait;
use App\Traits\VoucherTrait;

class FinanceInvoice extends BaseModel
    {
    use CommonUserTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "basedata_financeinvoice";
    protected $guarded = [];
    protected $appends = ['invoice_amount'];
    public $voucherkey = "code";
    public function details()
        {
        return $this->hasMany(FinanceInvoiceDetails::class, "main_id");
        }
    public function getInvoiceAmountAttribute()
        {
        return $this->amount * $this->state_id;
        }
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'OrderStatus');
            });
        }
    public function state()
        {
        return $this->belongsTo(Dic::class, "state_id", "key")->where(function ($q)
            {
            $q->where("code", 'invoice_state');
            });
        }


    public function unit()
        {
        return $this->belongsTo(Dic::class, "unit_id", "key")->where(function ($q)
            {
            $q->where("code", 'unit');
            });
        }
    public function company()
        {
        return $this->belongsTo(Company::class);
        }
    public function trade()
        {
        return $this->belongsTo(Trade::class);
        }


    public function finance($orders = null)
        {
        $v = $orders ?? $this;
        if ($v->status_id == 2)
            return $v->voucher;
        if ($v->status_id != 1) {
            fail("单据非审核状态！，无法财审！");
            }
        $details = [];
        $v->details->filter(function ($v) use (&$details)
            {
            $details[] = FinanceVoucherRepository::mergeVoucher($v->state_id == -1 ? "2202" : "1122", $v->finance_remark, 0, 0, array_merge($v->toArray(), [
                'custno'         => $v->material->name,
                'invoice_amount' => $v->amount
            ]), 0, 1);
            fauditSave($v);
            });
        $voucher = FinanceVoucherRepository::setACSign("INVOICE_CODE")->CreateVoucher($v->billdate ?? $v->created_at, 1, $details, $v, $v->voucher?->id);
        //审核凭证
        fauditSave($v);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    public function nofinance($orders = null)
        {
        $v = $orders ?? $this;
        if ($v->status_id == 1)
            return $v->voucher;
        if ($v->status_id != 2) {
            fail("非财审状态！，无法取消财审！");
            }
        $v->details->filter(function ($v)
            {
            fauditSave($v, false);
            });
        if ($orders == null && $v->voucher) {
            FinanceVoucherRepository::auditVoucher($v->voucher, false);
            FinanceVoucherRepository::RemoveVoucher($v->voucher);
            }
        return fauditSave($v, false);
        }
    }
